package com.wcgksoft.controller.order;

import com.wcgksoft.common.util.Layui;
import com.wcgksoft.common.util.MsgInfo;
import com.wcgksoft.entity.admin.Account;
import com.wcgksoft.entity.order.WithDrawOrder;
import com.wcgksoft.service.admin.AccountService;
import com.wcgksoft.service.order.WithDrawOrderService;
import javafx.beans.property.ReadOnlySetProperty;
import net.sf.json.JSONObject;
import org.apache.shiro.SecurityUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.persistence.ValidationMode;
import javax.persistence.criteria.CriteriaBuilder;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @author 御风
 * @version V1.0
 * @Package com.wcgksoft.controller.order
 * @Description TODO
 * @date 2020/5/21 16:08
 * @ClassName WithDrawOrderController
 */
@Controller
@RequestMapping("orderWith")
public class WithDrawOrderController {

    @Autowired
    private WithDrawOrderService withDrawOrderService;

    @Autowired
    private AccountService accountService;

    /**
     * 查询提现列表
     * @param page
     * @param limit
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "findAll")
    public Layui findAll(@RequestParam(value = "page",required = false)Integer page,
                         @RequestParam(value = "limit",required = false)Integer limit,
                         @RequestParam(value = "keyWord",required = false)String keyWord){
        Account account = (Account) SecurityUtils.getSubject().getSession().getAttribute("om_user");
        DetachedCriteria criteria = DetachedCriteria.forClass(WithDrawOrder.class);
        if (account.getAuthority().getRoleCode()=="agent"){
            criteria.add(Restrictions.eq("agent_id",account.getId()));
        }
        criteria.addOrder(Order.desc("withTime"));
        List<WithDrawOrder> list = withDrawOrderService.findByConditions(criteria, page, limit);
        return Layui.data(0, "success", withDrawOrderService.getTotalCount(), list);
    }

    @ResponseBody
    @RequestMapping(value = "findFinAll")
    public Layui findFinAll(@RequestParam(value = "page",required = false)Integer page,
                            @RequestParam(value = "limit",required = false)Integer limit,
                            @RequestParam(value = "keyWord",required = false)String keyWord){
        Account account = (Account) SecurityUtils.getSubject().getSession().getAttribute("om_user");
        DetachedCriteria criteria = DetachedCriteria.forClass(WithDrawOrder.class);
        criteria.add(Restrictions.eq("status",1));
        criteria.addOrder(Order.desc("withTime"));
        List<WithDrawOrder> list = withDrawOrderService.findByConditions(criteria, page, limit);
        return Layui.data(0, "success", withDrawOrderService.getTotalCount(), list);
    }

    //**************查询代理商提现金额****************
    @ResponseBody
    @RequestMapping(value = "findMoney")
    public MsgInfo findMoney(@RequestParam(value = "aid",required = false)Integer id){
        MsgInfo msgInfo = new MsgInfo();
        Account account = accountService.findById(id);
        msgInfo.setData(account);
        return msgInfo;
    }

    //**************代理商订单提现申请****************
    @ResponseBody
    @RequestMapping(value = "apply")
    public Layui applyTo(@RequestParam(value = "data",required = false)String data){
        JSONObject param = JSONObject.fromObject(data);
        if (param == null) {
            return Layui.data(1, "请求格式有误", 0, null);
        } else {
            WithDrawOrder withDrawOrder = (WithDrawOrder) JSONObject.toBean(JSONObject.fromObject(data), WithDrawOrder.class);
            Account account = (Account) SecurityUtils.getSubject().getSession().getAttribute("om_user");
            withDrawOrder.setStatus(0);
            withDrawOrder.setWithTime(new Date());
            withDrawOrderService.save(withDrawOrder);
            return Layui.data(0, "success", 1, Layui.addList(null));
        }
    }

    //**************总后台提现审核****************
    @ResponseBody
    @RequestMapping(value = "adminAgree")
    public Layui adminAgree(@RequestParam(value = "id",required = false)Integer id){
        WithDrawOrder withDrawOrder = withDrawOrderService.findById(id);
        withDrawOrder.setStatus(1);
        withDrawOrderService.saveMerge(withDrawOrder);
        return Layui.data(0, "success", 1, null);
    }

    @ResponseBody
    @RequestMapping(value = "adminRefuse")
    public Layui adminRefuse(@RequestParam(value ="data",required = false)String reason,
                             @RequestParam(value = "id",required = false)Integer id){
        WithDrawOrder withDrawOrder = withDrawOrderService.findById(id);
        withDrawOrder.setStatus(3);
        withDrawOrder.setReason(reason);
        withDrawOrderService.saveMerge(withDrawOrder);
        return Layui.data(0, "success", 1, null);
    }


    //**************财务提现付款****************
    @ResponseBody
    @RequestMapping(value = "confirm")
    public Layui financeConfirm(@RequestParam(value = "id",required = false)Integer id,
                                @RequestParam(value = "money",required = false)BigDecimal withMoney){
        WithDrawOrder withDrawOrder = withDrawOrderService.findById(id);
        Account account = accountService.findById(withDrawOrder.getAgent_id());
        BigDecimal subtract = account.getWithdrawalMoney().subtract(withMoney);
        account.setWithdrawalMoney(subtract);
        accountService.saveMerge(account);
        withDrawOrder.setStatus(4);
        withDrawOrderService.saveMerge(withDrawOrder);
        return Layui.data(0, "success", 1, null);
    }





}
